$(document).ready(function() {
	var $loginForm = $("#loginForm"), $submit = $loginForm.find(":submit");
	var $username = $("#username"), $password = $("#password");
	$submit.prop("disabled", false);
	$loginForm.validate({
		errorClass: "errorMessage",
		rules: {
			username: {
				required: true,
				pattern: /^[0-9a-z_A-Z\u4e00-\u9fa5]+$/,
				minlength: 2
			},
			password: {
				required: true,
				minlength: 4
			}
		},
		messages: {
			username: {
				required: "请输入用户名",
				pattern: "用户名只允许包含中文、英文、数字、下划线",
				minlength: "用户名最小长度为2"
			},
			password: {
				required: "请输入密码",
				minlength: "密码最小长度为4"
			}
		},
		submitHandler: function(form) {
			var url = $loginForm.attr('location')
			$submit.prop("disabled", true);
			//[#-- RSA加密 --]
			var rsaKey = new RSAKey();
			rsaKey.setPublic(b64tohex(modulus), b64tohex(exponent));
			//[#-- AJAX登录 --]
			$.ajax({
				url: url,
				data: {
					username: $username.val(),
					password: hex2b64(rsaKey.encrypt($password.val()))
				},
				type: "post",
				dataType: "json",
				cache: false,
				beforeSend: function(request, settings) {
					//[#-- 绑定令牌 --]
					request.setRequestHeader("token", $.cookie("token"));
					//[#-- 按钮显示操作 --]
					$submit.text(" 登 陆 中 ... ");
				},
				success: function(message) {
					if (message.type == "success"){
						window.location = message.successUrl;	
					} else {
						renderMessage(message);
						//[#-- 更新 --]
						$submit.text(" 登 陆 ");
						$submit.prop("disabled", false);
					}
				},
				error: function() {
					//[#-- 显示提示消息 --]
					renderMessage("登录失败")
					//[#-- 按钮显示操作 --]
					$submit.text(" 登 陆 失 败 ");
				}
			});
		}
	});
});





